import java.util.*;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class project {
    public int maxWidthOfVerticalArea(int[][] points) {
        Arrays.sort(points, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        int max=0;
        for(int i=0;i<points.length-1;i++){
            max=Math.max(points[i+1][0]-points[i][0],max);
        }
        return max;
    }












    public int[] processQueries(int[] queries, int m) {
        List<Integer> list=new ArrayList<>();
        for(int i=1;i<=m;i++){
            list.add(i);
        }
        int[] arr=new int[queries.length];
        for(int i=0;i<queries.length;i++){
            int tmp=queries[i];
            int index=-1;
            for(int j=0;j<m;j++){
                if(list.get(j)==tmp){
                    index=j;
                    break;
                }
            }
            arr[i]=index;
            list.remove(index);
            list.add(0,tmp);
        }
        return arr;
    }



















    int sum=0;
    public int sumEvenGrandparent(TreeNode root) {
        dfs(root);
        return sum;
    }
    public void dfs(TreeNode root){
        if(root==null){
            return ;
        }
        if(root.val%2==0){
            if(root.left!=null){
                if(root.left.left!=null){
                    sum+=root.left.left.val;
                }
                if(root.left.right!=null){
                    sum+=root.left.right.val;
                }
            }
            if(root.right!=null){
                if(root.right.left!=null){
                    sum+=root.right.left.val;
                }
                if(root.right.right!=null){
                    sum+=root.right.right.val;
                }
            }
        }
        dfs(root.left);
        dfs(root.right);
    }













    public int eatenApples(int[] apples, int[] days) {
        PriorityQueue<int[]> queue=new PriorityQueue<>((a,b)->a[0]-b[0]);
        int len=apples.length;
        int time=0;
        int ret=0;
        while (time<len||!queue.isEmpty()){
            if(time<len&&apples[time]>0) {
                queue.add(new int[]{
                        time + days[time] - 1, apples[time]
                });
            }
            while (!queue.isEmpty()&&queue.peek()[0]<time){
                queue.poll();
            }
            if(!queue.isEmpty()){
                int[] arr= queue.poll();
                if(--arr[1]>0&&arr[0]>time){
                    queue.add(arr);
                }
                ret++;
            }
            time++;
        }
        return ret;
    }
}
